package com.hebeu.gfh.controller;

import com.hebeu.gfh.pojo.po.College;
import com.hebeu.gfh.pojo.po.Major;
import com.hebeu.gfh.pojo.po.User;
import com.hebeu.gfh.pojo.vo.findMajor;
import com.hebeu.gfh.pojo.vo.majors;
import com.hebeu.gfh.service.collegeService;
import com.hebeu.gfh.service.majorService;
import com.hebeu.gfh.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 刘剑利
 * @Version: v1.0
 */
@RestController
@RequestMapping("/major")
public class majorController {
    @Autowired
    majorService majorService;
    @Autowired
    collegeService collegeService;
    //根据学院id查询专业
    @PostMapping("/findMajorByCoolegeId")
    public R findMajorByCoolegeId(int collegeId){
        List<Major> majorList =  majorService.findMajorByCoolegeId(collegeId);
        return R.ok("查询成功",majorList);
    }
    //添加专业
    @PostMapping("/addMajor")
    public R addMajor(Major major){
        try {
            majorService.addMajor(major);
        }catch (Exception e){
            System.out.println("异常为"+e);
            return R.error(500,"添加失败");
        }
        return R.ok("添加成功");
    }
    //删除专业
    @PostMapping("/deleteMajor")
    public R deleteMajor(int majorId){
        try {
            majorService.deleteMajor(majorId);
        }catch (Exception e){
            return R.error(500,"删除失败");
        }
        return R.ok("删除成功");
    }
    //根据id查询专业
    @PostMapping("/findMajorById")
    public R findMajorById(int majorId){
        Major major = majorService.findMajorById(majorId);
        return R.ok(major);
    }
    //查询专业下所有学生
    @PostMapping("/majorMember")
    public R majorMember(int majorId){
        List<User> userList = majorService.majorMember(majorId);
        return R.ok(userList);
    }
    //修改专业
    @PostMapping("/updateMajor")
    public R updateMajor(Major major){
        try {
            majorService.updateMajor(major);
        }catch (Exception e){
            return R.error(500,"修改失败");
        }
        return R.ok("修改成功");
    }

    //根据学院id查询相关的专业
    @PostMapping("/getMajorByCollegeId")
    public R getMajorByCollegeId(findMajor findMajor){
        System.out.println(findMajor);
        List<Major>  majorList = majorService.getMajorByCollegeId(findMajor);
        List<majors> majorsList = new ArrayList<>();
        for(Major major: majorList){
            List<User> userList = majorService.majorMember(major.getMajorId());
            College c = collegeService.findCollegeById(major.getCollegeId());
            String collegeName = c.getCollegeName();
            int num =userList.size();
            majors majorss = new majors();
            majorss.setMajor(major);
            majorss.setNum(num);
            majorss.setCollegeName(collegeName);
            majorsList.add(majorss);
        }
        return R.ok(majorsList);
    }
}
